﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PClusterModel.Infrastructure;

namespace PClusterModel.Utils
{
    public class DataLoader
    {
        public static MicroarrayDataMatrix LoadData(string microarrayDataFile)
        {
            StreamReader dataReader = new StreamReader(microarrayDataFile);
            Dictionary<string, int[]> intArrayData = ReadDataInIntArrayForm(dataReader);

            dataReader.Dispose();

            MicroarrayDataMatrix matrix = BuildMatrixFromData(intArrayData);
            return matrix;
        }

        private static MicroarrayDataMatrix BuildMatrixFromData(Dictionary<string, int[]> intArrayData)
        {
            MicroarrayDataMatrix matrix = new MicroarrayDataMatrix();

            foreach (KeyValuePair<string, int[]> dataPair in intArrayData)
            {
                Gene gene = ProduceGene(dataPair.Key, dataPair.Value);
                matrix.MicroarrayDataObjects.Add(gene);
            }
            return matrix;
        }

        private static Gene ProduceGene(string currentName, int[] expressionValues)
        {
            int length = expressionValues.Length;
            Gene gene = new Gene();
            gene.Name = currentName;
            for (int i = 0; i < length; i++)
            {
                gene.ExpresionValues.Add(i, expressionValues[i]);
            }
            return gene;
        }

        private static string[] ReadGeneNames(StreamReader geneNamesReader)
        {
            string content = geneNamesReader.ReadToEnd();
            geneNamesReader.Close();
            string[] geneNames = content.Split(new char[] { ' ', '\n', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            return geneNames;
        }

        private static Dictionary<string, int[]> ReadDataInIntArrayForm(StreamReader reader)
        {
            Dictionary<string, int[]> dataFromFile = new Dictionary<string, int[]>();
            string line = reader.ReadLine();
            while (line != null)
            {
                if (!string.IsNullOrEmpty(line))
                {
                    string[] tokens = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    if (tokens.Length > 0)
                    {
                        string name = tokens[0];
                        int[] intValues = ConvertToIntArray(tokens);
                        if (intValues.Length > 0)
                        {
                            dataFromFile.Add(name, intValues);
                        }
                    }
                }
                line = reader.ReadLine();
            }
            reader.Close();
            return dataFromFile;
        }

        private static int[] ConvertToIntArray(string[] stringTokens)
        {
            int length = stringTokens.Length;
            int[] intArray = new int[length - 1];

            for (int i = 1; i < length; i++)
            {
                string token = stringTokens[i];
                int convertedValue;
                int.TryParse(token, out convertedValue);
                intArray[i - 1] = convertedValue;
            }

            return intArray;
        }
    }
}
